package edu.fight.controller;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import edu.fight.dto.JwtUser;
import edu.fight.service.UserService;
import edu.fight.utils.RedisUtil;
import edu.fight.utils.RequestContextUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.util.ClassUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.support.RequestContextUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Arrays;
import java.util.Collections;
import java.util.Enumeration;

@RestController
@RequestMapping("/pro")
@Slf4j
public class IndexController {

    @Autowired
    UserService userService;


    /**
     * 查看打成jar包后运行的目录，打印项目运行方式（jar运行还是file运行），以及获取项目运行所在目录
     * @return
     */
    @GetMapping("/showjar")
    public String showjar() throws Exception {

        //第一种
        File path = new File(ResourceUtils.getURL("classpath:").getPath());
        if(!path.exists()) path = new File("");
        log.info(path.getAbsolutePath());
        //第二种
        log.info(System.getProperty("user.dir"));
        //第三种
        String path1 = ClassUtils.getDefaultClassLoader().getResource("").getPath();
        log.info(URLDecoder.decode(path1, "utf-8"));
        //第四种
        String path2 = ResourceUtils.getURL("classpath:").getPath();
        log.info(path2);

        log.info("==========准确分割线====");
        //第五种
        ApplicationHome h = new ApplicationHome(getClass());
        File jarF = h.getSource();
        log.info(jarF.getParentFile().toString());

        log.info("==========下面方法准确====");
        String protocol = IndexController.class.getResource("").getProtocol();
        File source = new ApplicationHome(IndexController.class).getDir();

        log.info(source.getAbsolutePath());
        log.info(protocol);

        userService.dobiz();

        return  null ;
    }


    @GetMapping("/show")
    public String show(HttpSession session){
        Enumeration<String> attributeNames = session.getAttributeNames();
        while (attributeNames.hasMoreElements()){
            String key = attributeNames.nextElement().toString();
            Object value = session.getAttribute(key);
            log.info(String.format("key : %s , value : %s ", key,value));

        }



        return "成功";
    }

    @Autowired
    RedisUtil redisUtil;

    @GetMapping("/getself")
    public String getself(){
//        SimpleAuthenticationInfo houlu = redisUtil.get("houlu");
//        log.info(JSONObject.toJSONString(houlu, SerializerFeature.PrettyFormat));

        return null ;
    }


}
